﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Xml;

namespace MethodWorx.CMS.TemplateParser
{
    public class TemplateElement : TemplateNode, IXmlElement
    {
        public int? ElementNameStringPosition { get; set; }
        public int? ElementEndValueStringPosition { get; set; }

        public string Namespace
        {
            get
            {
                if (this.Name.Contains(':'))
                    return this.Name.Split(':')[0];
                else
                {
                    return null;
                }
            }
        }

        public string LocalName
        {
            get
            {
                if (this.Name.Contains(':'))
                    return this.Name.Split(':')[1];
                else
                {
                    return null;
                }
            }
        }

        public bool HasNamespace
        {
            get { return !string.IsNullOrEmpty(this.Namespace); }
        }

        public bool HasAttribute(string name, string value, bool ignoreCase)
        {
            foreach(var child in this.Children.OfType<TemplateAttribute>())
            {
                if (ignoreCase && child.Name.ToLower() == name.ToLower() && child.Value.ToLower() == value.ToLower())
                    return true;
                else if (!ignoreCase && child.Name == name && child.Value == value)
                    return true;
            }
            return false;
        }

        public bool HasAttribute(string name, bool ignoreCase)
        {
            foreach (var child in this.Children.OfType<TemplateAttribute>())
            {
                if (ignoreCase && child.Name.ToLower() == name.ToLower())
                    return true;
                else if (!ignoreCase && child.Name == name)
                    return true;
            }
            return false;
        }

        public string GetAttributeValue(string name, string defaultIfMissing, bool ignoreCase)
        {
            foreach (var child in this.Children.OfType<TemplateAttribute>())
            {
                if (ignoreCase && child.Name.ToLower() == name.ToLower())
                    return child.Value;
                else if (!ignoreCase && child.Name == name)
                    return child.Value;
            }
            return defaultIfMissing;
        }

        public string GetAttributeValue(string name, string defaultIfMissing, bool ignoreCase, Dictionary<string, string> variables)
        {
            var value = defaultIfMissing;
            foreach (var child in this.Children.OfType<TemplateAttribute>())
            {
                if (ignoreCase && child.Name.ToLower() == name.ToLower())
                    value = child.Value;
                else if (!ignoreCase && child.Name == name)
                    value = child.Value;
            }

            if (value != null)
            {
                foreach(var variable in variables.Keys)
                {
                    value = value.Replace("{" + variable + "}", variables[variable]);
                }
            }

            return value;
        }

        public string InnerXml
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach(var element in this.Children)
                {
                    if (element is TemplateElement)
                    {
                        sb.Append(((TemplateElement)element).OuterXml);
                    }
                    else if (element is TemplateText)
                    {
                        sb.Append(((TemplateText)element).Text);
                    }
                }
                return sb.ToString();
            }
        }

        public string OuterXml
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<" + this.Name);
                foreach(var element in this.Children)
                {
                    //  we are going through all the children of this node
                    if (element is TemplateAttribute)
                    {
                        sb.Append(" " + ((TemplateAttribute) element).Name + "=\"" + ((TemplateAttribute) element).Value +
                                  "\"");
                    }
                }
                sb.Append(">");
                foreach(var element in this.Children)
                {
                    if(element is TemplateElement)
                    {
                        sb.Append(((TemplateElement)element).OuterXml);
                    }
                    else if(element is TemplateText)
                    {
                        sb.Append(((TemplateText) element).Text);
                    }
                }
                sb.Append("</" + this.Name + ">");
                return sb.ToString();
            }
        }


        public IEnumerable<TemplateAttribute> Attributes
        {
            get { return this.Children.OfType<TemplateAttribute>(); }
        }

        public TemplateAttribute AddAttribute(string name, string value)
        {
            var attr = new TemplateAttribute();
            attr.Name = name;
            attr.Value = value;
            this.AddNode(attr);

            return attr;
        }

        public void SetAttribute(string name, string value)
        {
            if (this.HasAttribute(name, true))
                this.Attributes.First(a => a.Name.ToLower() == name.ToLower()).Value = value;
            else
            {
                this.AddAttribute(name, value);
            }
        }


        public TemplateElement GetElement(string p)
        {
            return this.GetElements(p).FirstOrDefault();
        }

        public IEnumerable<TemplateElement> GetElements(string p)
        {
            return this.Children.OfType<TemplateElement>().Where(e => e.Name == p);
        }

        public IEnumerable<TemplateElement> Elements
        {
            get { return this.Children.OfType<TemplateElement>(); }
        }

        public bool HasClosingTag { get; set; }





        public string GetInnerXml(Dictionary<string, string> variables)
        {
            var value = this.InnerXml;
            if (value != null)
            {
                foreach (var variable in variables.Keys)
                {
                    value = value.Replace("{" + variable + "}", variables[variable]);
                }
            }

            return value;
        }

        public void Append(string input)
        {
            var xmlparser = new XmlParser(input);
            this.AddNodes(xmlparser.Document.FlattenNodes<TemplateNode>());
        }
    }


}
